package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Question;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.QuestionService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/store/question/*")
public class QuestionServlet extends BaseServlet {
    private QuestionService questionService = BeanFactory.getBean("questionService",QuestionService.class);

    private Result upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            List<FileItem> fileItems = fileUpload.parseRequest(request);
            FileItem fileItem = fileItems.get(0);
            String realPath = getServletContext().getRealPath("/upload");
            String fileName = fileItem.getName();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String substring = fileName.substring(fileName.lastIndexOf("."));
            fileName=uuid+substring;
            fileItem.write(new File(realPath,fileName));
            return new Result(true,MessageConstant.UPLOAD_QUESTIONIMG_SUCCESS,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPLOAD_QUESTIONIMG_FAIL);
        }
    }
    
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            
            List<Question> list = questionService.findAll();
            return new Result(true,MessageConstant.QUERY_QUESTION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_QUESTION_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            
            questionService.delete(ids);
            return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }
    }
    private Result toExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            response.setHeader("content-disposition","attachment;filename=question.xlsx");
            questionService.toExport(response.getOutputStream());
            return new Result(true,MessageConstant.QUESTION_EXPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUESTION_EXPORT_FAIL);
        }
    }
    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Question question = BeanUtil.fillBeanFromJson(request, Question.class);
            questionService.update(question);
            return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String id = request.getParameter("id");
            
            Question question = questionService.findById(id);
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,question);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            Question question = BeanUtil.fillBeanFromJson(request, Question.class);
            
            questionService.save(question);
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }
    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            String currentPageStr = request.getParameter("currentPage");
            String pageSizeStr = request.getParameter("pageSize");
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageStr)){
                currentPage=Integer.parseInt(currentPageStr);
            }
            if (!StringUtils.isBlank(pageSizeStr)){
                pageSize=Integer.parseInt(pageSizeStr);
            }
            
            PageInfo<Question> info =questionService.findByPage(currentPage,pageSize);
            return new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMPANY_FAIL);
        }

    }

    
}
